package dacp.etl.kafka.hdfs.tools;

import java.net.URI;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileStatus;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.LocatedFileStatus;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.fs.RemoteIterator;
import org.apache.hadoop.security.UserGroupInformation;

public class KerberosValid {
	
	private static String principal = "hdfs-hbbdch@HBBDCH.COM";
	private static String nnprincipal = "nn/_HOST@HBBDCH.COM";
	private static String  keytab ="";
	private static String hdfsUrl="hdfs://hbbdch-nn-01:8020";
	private static String krb5ConfFile ="";

	public static void main(String[] args) throws Exception {
		

		Configuration conf = new Configuration();
		
		if(args.length > 0 ){
			hdfsUrl = args[0];
		}
		System.out.println("hdfs-url  :" + hdfsUrl);
		if(args.length > 1 ){
			principal = args[1];
			System.out.println("principal :" + principal);
			if(args.length > 2){
				keytab = args[2];
			}

			System.out.println("keytab    :" + keytab);
			if(args.length > 3){
				krb5ConfFile =args[3];
				System.setProperty("java.security.krb5.conf", krb5ConfFile);
			}
			System.out.println("krb5ConfF :" + krb5ConfFile);
			
			if(args.length> 4){
				nnprincipal = args[4];
				conf.set("dfs.namenode.kerberos.principal", nnprincipal);
			}
			System.out.println("nnprincipal :" + nnprincipal);
			
			conf.set("hadoop.security.authentication", "kerberos");
	        conf.set("hadoop.security.authorization", "true");
	        
	        UserGroupInformation.setConfiguration(conf);
	        UserGroupInformation.loginUserFromKeytab(principal, keytab);

		}
		
		
		
        
        
        FileSystem fs = FileSystem.newInstance(URI.create(hdfsUrl), conf);
        
        System.out.println("=============");
        FileStatus[] listStatus = fs.listStatus(new Path(hdfsUrl+"/"));
        for(FileStatus  f :listStatus){
        	System.out.println(f.getPath().toString());
        }
         
        System.out.println("=============> " + listStatus.length);
        
	}
}
